//
//  CPUserAccount.swift
//  weibo
//
//  Created by zcp on 16/5/13.
//  Copyright © 2016年 zcp. All rights reserved.
//

import UIKit

class CPUserAccount: NSObject {
    /// 用户授权的位移票据
    var access_token: String?
    /// access_token的生命周期，单位是秒数, 基本数据类型不能使用?,KVC会找不到
    var expires_in: NSTimeInterval = 0{
    
        didSet{
        
            expriseDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    /// 授权用户的UID
    var uid: String?
    /// 方便后期用的时候比较时间
    var expriseDate: NSDate?
    ///用户昵称
    var screen_name: String?
    ///用户头像地址（大图），180×180像素
    var avatar_large: String?
    //通过字典转模型
    init(dict: [String : AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    //赋值key和属性不匹配时崩
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    //打印时会调用这个属性
    override var description: String{
    
        get{return "用户账户: access_token: \(access_token), expires_in: \(expires_in), uid: \(uid)"}
    
    }

    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expriseDate, forKey: "expriseDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expriseDate = aDecoder.decodeObjectForKey("expriseDate") as? NSDate
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }

}
